<template>
  <div class="box">
    <div class="bg"></div>
    <van-nav-bar id="van-bar" :title="title" :border="border" left-arrow @click-left="onClickLeft"
      @click-right="onClickRight">
      <template #left>
        <img :style="{ width: widthPx, height: heightPx }" src="@/assets/iconPerfectInformation/nav_back@2x.png" />
      </template>
      <template #right >
       <span v-if="isshow1"> 重新申请</span>
       <!-- <span v-if="!isshow1">取消</span> -->
      </template>
     
    </van-nav-bar>

    <div class="album" :style="'top:' +
      (topheight + 60) +
      'px;height:calc(100vh - ' +
      (topheight + 60) +
      'px);width:100%;overflow:auto;'
      ">
      <div class="top" :style="'top:' +
        (topheight) + 'px;'" v-if="isshow">
        <div class="sming">请先完成实名认证后，再申请成为商家</div>
        <div class="renzheng" @click="goto">去认证</div>
      </div>
      <div class="albumlist">

        <div class="title">我的手机号</div>
        <div class="input">
          <input placeholder="填写手机号" :disabled='isshow1' v-model="phone" type="text">
        </div>
        <div class="title1">手机号人工审核，请填写真实手机号</div>
        <div class="title">我的微信号</div>
        <div class="input">
          <input placeholder="填写微信号" :disabled='isshow1' v-model="wechat" type="text">
        </div>
        <div class="title1">微信号人工审核，请填写真实微信号</div>
        <div class="title3">
          <div class="title">常出没于</div>
          <div class="input2"><input placeholder="输入常出没地点" :disabled='isshow1' v-model="haunt_place" type="text"></div>
        </div>
        <div class="title4">
          <div class="title">服务项目</div>
          <div class="input3" v-if="!isshow1" @click="addLabel('service')"><img src="@/assets/myicon/tag-add-purple.png"
              alt="" srcset="">添加服务</div>
        </div>
        <div class="list">
          <div class="tab" v-for="(item, index) in list" :key="index"><span>{{ item }}</span><img
              src="@/assets/myicon/tag-del-purple.png" @click="dellist(index)" v-if="!isshow1" alt="" srcset=""></div>
        </div>
        <!-- <p style="text-align: center;margin: 24px 0;color: #AAAAAA;font-size: 10px;"></p> -->
      </div>
    </div>
    <div class="bnt" v-if="!isshow1">
      <van-button type="primary" block @click="Apply"
        color="linear-gradient(180deg, #3764EC 0%, #6B85F7 100%)">确认申请</van-button>
    </div>

    <addLabel @closeLabelViewPop="closeLabelViewPop" :showLabelViewPop="showLabelViewPop" :type="labelType"></addLabel>
  </div>
</template>

<script>
import { applybusiness, getimpressInfo } from "@/api/index"
import addLabel from '@/components/addLabel.vue'
export default {
  components: {
    addLabel,
  },
  data() {
    return {
      border: "",
      widthPx: "28px",
      heightPx: "28px",
      topheight: 0,
      isActive: 0,
      isShow: false,
      title: '成为商家',
      isActive: 0,
      file: '',
      id: '',
      isshow: false,
      isshow1: false,
      loading: false,
      finished: false,
      wechat: '',
      phone: '',
      service_label: [],
      haunt_place: '',
      showLabelViewPop: false,
      labelType: '',
      deldata: {
        album: ''
      },
      adddata: {
        albums: []
      },

      list: [

      ],
      listid:[

      ]
    };
  },
  mounted() {
    console.log(this.$store.state.userInfo.is_merchant);
    if (this.$store.state.userInfo.is_authentication == 0) {
      this.isshow = true
    }
    console.log(this.$store.state);
    if (this.$store.state.userInfo.is_merchant == 1) {
      this.getImpressInfo()
        this.isshow1 = true

      this.type = 'edit'
    } else {
      this.type = 'add'
    }
    this.topheight = document.getElementById("van-bar").offsetHeight;
    this.$nextTick(() => {


    })
  },

  methods: {
    dellist(index){
      var arr = this.listid;
      var arr2 = this.list;
      console.log(arr);
      console.log(this.listid);
arr.splice(index, 1); 
arr2.splice(index, 1);

    },
    goto(){
      this.$router.push('/attestation')
    },
    getImpressInfo() {
      getimpressInfo({ ...this.adddata }
      ).then((res) => {
        this.id = res.data.id
        this.wechat = res.data.wechat
        this.phone = res.data.phone
        this.haunt_place = res.data.haunt_place
        this.list = res.data.service_label
        console.log(res);
        this.isshow1 = true

        // 加载状态结束

      })
    },
    closeLabelViewPop(data) {
      console.log(data)
      var list1 = []
      var ids = []
      data.data.forEach((el) => {
        list1.push(el.label_name)
        ids.push(el.id)
      })
      this.listid = ids;
      this.list = list1
      this.showLabelViewPop = false

    },
    addLabel(type) {
      this.labelType = type
      this.showLabelViewPop = true
    },
    onClickRight() {
      this.isshow1 = !this.isshow1
      
       this.wechat = ''
        this.phone = ''
        this.haunt_place = ''
        this.list = ''
    },
    del(e) {

      delAlbumList(e
      ).then((res) => {

        console.log(res);
        // 加载状态结束

      })
    },
    uploadImgSuccess(value) {
      this.adddata.albums = [...this.list, value.url]
      console.log(this.adddata);
      addAlbumList({ ...this.adddata }
      ).then((res) => {

        console.log(res);
        // 加载状态结束

      })
      this.isBuddhaShow = false;
      this.isShow = false;
    },
    beforeRead(file) {
      this.isShow = false;
      if (file.type !== 'image/jpeg' && file.type !== 'image/png') {
        this.$toast('请上传 jpg、png 格式图片');
        return false;
      }
      this.$nextTick(() => {
        this.file = file;
        console.log(this.file);
        this.isShow = true;
      })
      return true;
    },
    onClickLeft() {
      this.$router.back();
    },
    handleUploadSuccess(e) {
      console.log(e);
    },
    add() {
      if(!this.phone){
        this.$toast('请输入您的手机号');
        return;
      }
      if(!this.wechat){
        this.$toast('请输入您的微信号');
        return;
      }
      if(!this.haunt_place){
        this.$toast('请输入您常出没的地方');
        return;
      }
      if(this.list.length<=0){
        this.$toast('请选择服务项目');
        return;
      }
      let data = {
        wechat: this.wechat,
        phone: this.phone,
        service_label: this.listid,
        haunt_place: this.haunt_place
      }
      console.log(data);
      applybusiness(data
      ).then((res) => {
        this.$toast('申请成功！');
        this.getImpressInfo()
        // this.list = list.concat(...res.data.list)
        console.log(res);
        // 加载状态结束
      })
    },
    edit() {
      if(!this.phone){
        this.$toast('请输入您的手机号');
        return;
      }
      if(!this.wechat){
        this.$toast('请输入您的微信号');
        return;
      }
      if(!this.haunt_place){
        this.$toast('请输入您常出没的地方');
        return;
      }
      if(this.list.length<=0){
        this.$toast('请选择服务项目');
        return;
      }
      let data = {
        // id: this.id,
        phone: this.phone.toString(),
        wechat: this.wechat.toString(),
        service_label: this.listid,
        haunt_place: this.haunt_place.toString()
      }
      console.log(data);

      applybusiness(data
      ).then((res) => {
        this.$toast('成功！');
        // this.list = list.concat(...res.data.list)
        this.getImpressInfo()
        this.isshow1=true
        console.log(res);
        // 加载状态结束
      })
    },
    Apply() {
      if(this.isshow==true){
        this.$toast('请先完成实名认证后，再申请成为商家');
        return
      }
      // if (this.type == 'add') {
        this.add()
      // } else if (this.type == 'edit') {
      //   this.edit()
      // }
    },
   
    onclose() {
      this.isShow = false;
    },
    
  },
};
</script>

<style scoped lang="scss">
.input input {
  width: 343px;
  height: 60px;
  display: block;
  background: rgba(107, 133, 247, 0.3);
  border-radius: 10px;
  box-sizing: border-box;
  padding: 0 20px;
  font-size: 14px;
  border-color: rgba(82, 117, 241, 0.30);
}

.title {
  margin: 8px 0;
  font-size: 16px;
  font-weight: 600;
  color: rgba(26, 26, 26, 1);
}

.title1 {
  color: rgba(118, 118, 118, 1);
  font-size: 12px;
  padding-left: 10px;
  padding-top: 9px;
  margin-bottom: 25px;
}

.title3 {
  display: flex;
  line-height: 40px;
  height: 40px;
  margin-bottom: 30px;
  border-bottom: 1px rgba(216, 216, 216, 0.50) solid;
  justify-content: space-between;
}

.title3 .title {
  height: 40px;
  margin: 0;
}

.title3 input {
  height: 40px;
  text-align: right;
  border: 0;
}

.title4 {
  display: flex;
  line-height: 40px;
  height: 40px;
  justify-content: space-between;
}

.title4 .title {
  margin: 0;
}

.renzheng {
  background-color: rgba(250, 81, 81, 1);
  width: 52px;
  height: 20px;
  margin: 5px 8px;
  text-align: center;
  line-height: 20px;
  border-radius: 10px;
  color: #FFFFFF;
}

.input3 {
  width: 71px;
  height: 20px;
  margin: 10px 0;
  border: 1px;
  background-color: rgba(231, 212, 255, 1);
  color: #8A38F5;
  line-height: 20px;
  font-size: 12px;
  text-align: center;
  border: 1px solid rgba(138, 56, 245, 1);
  border-radius: 10px;
}

.input3 img,
.tab img {
  width: 12px;
  height: 12px;
  vertical-align: text-bottoms;
}

.tab {
  margin-right: 8px;
  margin-bottom: 5px;
  background-color: rgba(231, 212, 255, 1);
  padding: 0 7px;
  border-radius: 10px;
}

.top {
  position: absolute;
  display: flex;
  height: 30px;
  line-height: 30px;
  justify-content: center;
  top: 0;
  left: 0;
  background-color: rgba(252, 219, 219, 1);
  width: 100%;
}

.sming {}

.tab span {
  line-height: 21px;
  vertical-align: top;
  color: rgba(138, 56, 245, 1);
}

.tab img {
  display: inline-block;
  margin: 4px 5px;
  margin-right: 0;
  width: 12px;
  height: 12px;
}

.bnt {
  width: 343px;
  height: 50px;
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: linear-gradient(180deg, #3764EC 0%, #6B85F7 100%);
  border-radius: 8px;
  font-size: 16px;
  font-family: PingFangSC, PingFang SC;
  font-weight: 500;
  text-align: center;
  color: #FFFFFF;
  line-height: 50px;
}

.glance {
  position: absolute;
}

.bg {
  width: 375px;
  height: 88px;
  position: absolute;
  top: 0;
  background: linear-gradient(180deg, #CDE1FD 0%, rgba(255, 255, 255, 0) 100%);
}

.albumlist {
  width: 345px;
  position: relative;
  margin: 40px auto;
}

.list {
  display: flex;
  margin-top: 10px;
  flex-wrap: wrap;
}



.van-cell {
  background-color: transparent;
}

.van-cell::after {
  border-color: #d8d8d8;
}

.van-cell__value {
  color: #1a1a1a;
  font-weight: 500;
}

::v-deep .van-uploader {
  display: block !important;

  & .van-uploader__wrapper {
    display: block !important;
  }
}</style>
